package com.ruoyi.web.controller.common;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.SysKeyNz;
import com.ruoyi.system.domain.SysUserAccount;
import com.ruoyi.system.mapper.SysKeyNzMapper;
import com.ruoyi.system.mapper.SysUserAccountMapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/commonKey")
public class CommonKey {

    @Resource
    private SysUserAccountMapper sysUserAccountMapper;
    @Resource
    private SysKeyNzMapper sysKeyNzMapper;


    @PostMapping(value = "/addData")
    @Transactional
    public AjaxResult addData(String type, String timeType) {

        return AjaxResult.success("添加成功~");
    }

    private void updateDiDi(String timeType){
        List<SysKeyNz> sysKeyNzs = sysKeyNzMapper.selectAllKey();
        for (SysKeyNz sysKeyNz : sysKeyNzs) {
            if (sysKeyNz.getExpirationTime() != null) {
                switch (timeType) {
                    case "0": // 一天
                        try {
                            sysKeyNz.setExpirationTime(addDate(sysKeyNz.getExpirationTime(), 1));
                        } catch (ParseException e) {
                            throw new RuntimeException(e);
                        }
                        break;
                    case "1": // 两天
                        try {
                            sysKeyNz.setExpirationTime(addDate(sysKeyNz.getExpirationTime(), 2));
                        } catch (ParseException e) {
                            throw new RuntimeException(e);
                        }
                        break;
                    case "2": // 三天
                        try {
                            sysKeyNz.setExpirationTime(addDate(sysKeyNz.getExpirationTime(), 3));
                        } catch (ParseException e) {
                            throw new RuntimeException(e);
                        }
                        break;
                    case "3": // 四天
                        try {
                            sysKeyNz.setExpirationTime(addDate(sysKeyNz.getExpirationTime(), 4));
                        } catch (ParseException e) {
                            throw new RuntimeException(e);
                        }
                        break;
                    case "4": // 五天
                        try {
                            sysKeyNz.setExpirationTime(addDate(sysKeyNz.getExpirationTime(), 5));
                        } catch (ParseException e) {
                            throw new RuntimeException(e);
                        }
                        break;
                    case "5": // 一周
                        try {
                            sysKeyNz.setExpirationTime(addDate(sysKeyNz.getExpirationTime(), 7));
                        } catch (ParseException e) {
                            throw new RuntimeException(e);
                        }
                        break;
                }
                // 修改时间
                sysKeyNzMapper.updateSysKeyNz(sysKeyNz);
            }
        }
    }
    private void updateHallo(String timeType) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 查询当前卡密
        List<SysUserAccount> sysKeyList = sysUserAccountMapper.selectAllAccount();
        for (SysUserAccount sysKey : sysKeyList) {
            if (sysKey.getEndTime() != null) {
                // 获取到期时间
                Date parse;
                try {
                    parse = sdf.parse(sysKey.getEndTime());
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                switch (timeType) {
                    case "0": // 一天
                        try {
                            sysKey.setEndTime(sdf.format(addDate(parse, 1)));
                        } catch (ParseException e) {
                            throw new RuntimeException(e);
                        }
                        break;
                    case "1": // 两天
                        try {
                            sysKey.setEndTime(sdf.format(addDate(parse, 2)));
                        } catch (ParseException e) {
                            throw new RuntimeException(e);
                        }
                        break;
                    case "2": // 三天
                        try {
                            sysKey.setEndTime(sdf.format(addDate(parse, 3)));
                        } catch (ParseException e) {
                            throw new RuntimeException(e);
                        }
                        break;
                    case "3": // 四天
                        try {
                            sysKey.setEndTime(sdf.format(addDate(parse, 4)));
                        } catch (ParseException e) {
                            throw new RuntimeException(e);
                        }
                        break;
                    case "4": // 五天
                        try {
                            sysKey.setEndTime(sdf.format(addDate(parse, 5)));
                        } catch (ParseException e) {
                            throw new RuntimeException(e);
                        }
                        break;
                    case "5": // 一周
                        try {
                            sysKey.setEndTime(sdf.format(addDate(parse, 7)));
                        } catch (ParseException e) {
                            throw new RuntimeException(e);
                        }
                        break;
                }
                sysUserAccountMapper.updateSysUserAccount(sysKey);
            }
        }
    }

    public static Date addDate(Date d, long day) throws ParseException {

        long time = d.getTime();
        day = day * 24 * 60 * 60 * 1000;
        time += day;
        return new Date(time);

    }


}
